<?php
// +----------------------------------------------------------------------
// | Exchanges
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Api\Logic;

/**
 * 小哥业务逻辑
 *
 * @author derek <693472824@qq.com>
 */
class WorkerLogic extends BaseLogic {
    
    /**
     * 获取今天及一周内的排班表
     */
    public function schedule($workerID='', $appointment = ''){
        
        $appointList = array();
        
        if(!empty($workerID)){
            
            $map['worker_id'] = $workerID;
            $map['appointment'] = array('GT', NOW_TIME + 59 * 60);      //当前时间点的下一整点
            $map['status'] = '2';                                       //已确定订单
            
            $appointList = M('Orders')->where($map)->getField('appointment', true);
        }
        
        $period = C('WORKING_HOURS');
        list($start, $end) = strpos($period, '-') ? explode('-', $period) : explode('-', '8-20');
        
        $dateList = array();
        
        for($i=0; $i<7; $i++){
            
            $datetime = strtotime('+'.$i.'days', NOW_TIME);
            $timestamp = GetMkTime(date('Y-m-d', $datetime));
            $timeList = array();
            
            for($s=$start,$e=$end; $s<=$e; $s++){
                
                $hour = strtotime('+'.$s.'hours', $timestamp);
                
                //1:已过期；2：已有预约；3：已选中
                if($hour <= (NOW_TIME + 1800)){
                    $time['status'] = '1';
                }else if(count($appointList) > 0 && in_array($hour, $appointList)){
                    $time['status'] = '2';
                }else if($hour == $appointment){
                    $time['status'] = '3';
                }else{
                    $time['status'] = '4';
                }
                
                $time['timestamp'] = $hour;
                $timeList[] = $time;
            }
            
            $date['timestamp'] = $timestamp;
            $date['timeList'] = $timeList;
            $date['tlist'] = json_encode($timeList);
            
            $dateList[] = $date;
        }
        
        return $dateList;
    }
}

?>